{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Built-in Datatypes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This first block contains imports used in later exercises"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import copy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 4\n",
    "b = 3.1415\n",
    "c = 1.0\n",
    "d = 2 + 4j\n",
    "e = \"Hello\"\n",
    "f = \"World\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 2\n",
    "\n",
    "Note that many solutions make use of f-strings to improve the output.\n",
    "See the chapter on strings for more on f-strings. In a nutshell, and f-string\n",
    "lets you mix a string with code, where the code is always surrounded by curly\n",
    "braces `{`*code*`}`. An f-string is equivalent to adding a string with the\n",
    "`str` of an object. For example, the first solution `print(f\"a: {a}\")` is\n",
    "the same as `print(\"a: \" + str(a))`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "print(f\"a: {a}\")\n",
    "print(f\"type(a): {type(a)}\")\n",
    "\n",
    "print(f\"a: {b}\")\n",
    "print(f\"type(a): {type(b)}\")\n",
    "\n",
    "print(f\"a: {c}\")\n",
    "print(f\"type(a): {type(c)}\")\n",
    "\n",
    "print(f\"a: {d}\")\n",
    "print(f\"type(a): {type(d)}\")\n",
    "\n",
    "print(f\"a: {e}\")\n",
    "print(f\"type(a): {type(e)}\")\n",
    "\n",
    "print(f\"a: {f}\")\n",
    "print(f\"type(a): {type(f)}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "# Added\n",
    "print(f\"a + b: {a + b}\")\n",
    "print(f\"a + c: {a + c}\")\n",
    "print(f\"a + d: {a + d}\")\n",
    "print(f\"b + c: {b + c}\")\n",
    "print(f\"b + d: {b + d}\")\n",
    "print(f\"c + d: {c + d}\")\n",
    "print(f\"e + f: {e + f}\")\n",
    "# Subtracted - No e - f\n",
    "print(f\"a - b: {a - b}\")\n",
    "print(f\"a - c: {a - c}\")\n",
    "print(f\"a - d: {a - d}\")\n",
    "print(f\"b - c: {b - c}\")\n",
    "print(f\"b - d: {b - d}\")\n",
    "print(f\"c - d: {c - d}\")\n",
    "# Multiplied - Note e *a, f * a\n",
    "print(f\"a * b: {a * b}\")\n",
    "print(f\"a * c: {a * c}\")\n",
    "print(f\"a * d: {a * d}\")\n",
    "print(f\"b * c: {b * c}\")\n",
    "print(f\"b * d: {b * d}\")\n",
    "print(f\"c * d: {c * d}\")\n",
    "print(f\"e * a: {e * a}\")\n",
    "print(f\"f * a: {f * a}\")\n",
    "# Divided\n",
    "print(f\"a / b: {a / b}\")\n",
    "print(f\"a / c: {a / c}\")\n",
    "print(f\"a / d: {a / d}\")\n",
    "print(f\"b / c: {b / c}\")\n",
    "print(f\"b / d: {b / d}\")\n",
    "print(f\"c / d: {c / d}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "print(f\"type(a + b): {type(a + b)}\")\n",
    "print(f\"type(a + c): {type(a + c)}\")\n",
    "print(f\"type(a + d): {type(a + d)}\")\n",
    "print(f\"type(b + c): {type(b + c)}\")\n",
    "print(f\"type(b + d): {type(b + d)}\")\n",
    "print(f\"type(c + d): {type(c + d)}\")\n",
    "print(f\"type(e + f): {type(e + f)}\")\n",
    "# Subtracted - No e - f\n",
    "print(f\"type(a - b): {type(a - b)}\")\n",
    "print(f\"type(a - c): {type(a - c)}\")\n",
    "print(f\"type(a - d): {type(a - d)}\")\n",
    "print(f\"type(b - c): {type(b - c)}\")\n",
    "print(f\"type(b - d): {type(b - d)}\")\n",
    "print(f\"type(c - d): {type(c - d)}\")\n",
    "# Multiplied - Note e *a, f * a\n",
    "print(f\"type(a * b): {type(a * b)}\")\n",
    "print(f\"type(a * c): {type(a * c)}\")\n",
    "print(f\"type(a * d): {type(a * d)}\")\n",
    "print(f\"type(b * c): {type(b * c)}\")\n",
    "print(f\"type(b * d): {type(b * d)}\")\n",
    "print(f\"type(c * d): {type(c * d)}\")\n",
    "print(f\"type(e * a): {type(e * a)}\")\n",
    "print(f\"type(f * a): {type(f * a)}\")\n",
    "# Divided\n",
    "print(f\"type(a / b): {type(a / b)}\")\n",
    "print(f\"type(a / c): {type(a / c)}\")\n",
    "print(f\"type(a / d): {type(a / d)}\")\n",
    "print(f\"type(b / c): {type(b / c)}\")\n",
    "print(f\"type(b / d): {type(b / d)}\")\n",
    "print(f\"type(c / d): {type(c / d)}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "ex = \"Python is an interesting and useful language for numerical computing!\"\n",
    "print(f\"ex[:6]: {ex[:6]}\")\n",
    "print(f\"ex[0:6]: {ex[0:6]}\")\n",
    "print(f\"ex[:-63]: {ex[:-63]}\")\n",
    "print(f\"ex[-69:-63]: {ex[-69:-63]}\")\n",
    "print(f\"ex[-1:]: {ex[-1:]}\")\n",
    "print(f\"ex[68]: {ex[68]}\")\n",
    "print(f\"ex[68:]: {ex[68:]}\")\n",
    "print(f\"ex[-10:-1]: {ex[-10:-1]}\")\n",
    "print(f\"ex[59:68]: {ex[59:68]}\")\n",
    "print(f\"ex[21:23]: {ex[21:23]}\")\n",
    "print(f\"ex[-48:-46]: {ex[-48:-46]}\")\n",
    "print(f\"ex[::-1]: {ex[::-1]}\")\n",
    "print(f\"ex[68::-1]: {ex[68::-1]}\")\n",
    "print(f\"ex[5::-1]: {ex[5::-1]}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 6\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "print(f\"(a,): {(a,)}\")\n",
    "print(f\"tuple([a]): {tuple([a])}\")\n",
    "print(f\"[a]: {[a]}\")\n",
    "print(f\"list((a,)): {list((a,))}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 7\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = [[1, 0.5], [0.5, 1]]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 8\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "y = x\n",
    "print(f\"y: {y}\")\n",
    "print(f\"x: {x}\")\n",
    "y[0][0] = 1.61\n",
    "print(f\"y: {y}\")\n",
    "print(f\"x: {x}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 9\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "z = x[:]\n",
    "y[0][0] = 1j\n",
    "print(f\"x: {x}\")\n",
    "print(f\"y: {y}\")\n",
    "print(f\"z: {z}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 10\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "w = copy.deepcopy(x)\n",
    "w[0][0] = -1\n",
    "print(f\"w: {w}\")\n",
    "print(f\"x: {x}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 11\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "x = [4, 3.1415, 1.0, 2 + 4j, \"Hello\", \"World\"]\n",
    "# Copy since single level and immutable data\n",
    "y = x[:]\n",
    "print(f\"y: {y}\")\n",
    "print(f\"y.pop(2): {y.pop(2)} \")\n",
    "print(f\"y: {y}\")\n",
    "y = x[:]\n",
    "del y[2]  # NOPRINT\n",
    "print(f\"y: {y}\")\n",
    "y = x[:]\n",
    "print(f\"y.remove(1.0): {y.remove(1.0)}\")\n",
    "print(f\"y: {y}\")\n",
    "print(f\"x.extend([1.0, 2+4j, 'Hello']): {x.extend([1.0, 2+4j, 'Hello'])}\")\n",
    "print(f\"x: {x}\")\n",
    "# Makes a copy\n",
    "print(f\"x[::-1]: {x[::-1]}\")\n",
    "# Inplace\n",
    "print(f\"x.reverse(): {x.reverse()}\")\n",
    "print(f\"x.count('Hello'): {x.count('Hello')}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 12\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "x = {\"alpha\": 1.0, \"beta\": 3.1415, \"gamma\": -99}\n",
    "print(f\"x: {x}\")\n",
    "print(f\"x['alpha']: {x['alpha']}\")\n",
    "\n",
    "x = [4, 3.1415, 1.0, 2 + 4j, \"Hello\", \"World\"]\n",
    "print(f\"x: {x}\")\n",
    "x.extend([1.0, 2 + 4j, \"Hello\"])\n",
    "print(f\"After running x.extend([1.0, 2+4j, 'Hello']): {x}\")\n",
    "# Set removes duplicates\n",
    "x = set(x)\n",
    "print(f\"Using set(x) to remove deplicates: {set(x)}\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
